﻿using System.Threading.Tasks;

namespace Sidvall.IdentityManager.UI.Entities
{
	public partial class Plugin
	{
		#region Public Members

		#region Title

		public string Title
		{
			get
			{
				if (this.PluginInstance == null)
					return string.Empty;
				return this.PluginInstance.Title;
			}
		}

		#endregion
		#region DataColumnCaption1

		public string DataColumnCaption1
		{
			get
			{
				if (this.PluginInstance == null)
					return "MISSING";
				return this.PluginInstance.DataColumnCaption1;
			}
		}

		#endregion
		#region DataColumnCaption2

		public string DataColumnCaption2
		{
			get
			{
				if (this.PluginInstance == null)
					return "MISSING";
				return this.PluginInstance.DataColumnCaption2;
			}
		}

		#endregion
		#region DataColumnCaption3

		public string DataColumnCaption3
		{
			get
			{
				if (this.PluginInstance == null)
					return "MISSING";
				return this.PluginInstance.DataColumnCaption3;
			}
		}

		#endregion
		#region ProcessFilter

		public bool ProcessFilter
		{
			get
			{
				if (!this.FilterIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				return true;
			}
		}

		#endregion
		#region ShowEditView

		public bool ShowEditView
		{
			get
			{
				if (!this.EditIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				if (this.PluginInstance.EditViewModelType == null)
					return false;
				return true;
			}
		}

		#endregion
		#region ShowFilterView

		public bool ShowFilterView
		{
			get
			{
				if (!this.FilterIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				if (this.PluginInstance.FilterViewModelType == null)
					return false;
				return true;
			}
		}

		#endregion
		#region ShowDataColumn1

		public bool ShowDataColumn1
		{
			get
			{
				if (!this.FilterIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				if (this.PluginInstance.DataColumnCaption1 == null)
					return false;
				return true;
			}
		}

		#endregion
		#region ShowDataColumn2

		public bool ShowDataColumn2
		{
			get
			{
				if (!this.FilterIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				if (this.PluginInstance.DataColumnCaption2 == null)
					return false;
				return true;
			}
		}

		#endregion
		#region ShowDataColumn3

		public bool ShowDataColumn3
		{
			get
			{
				if (!this.FilterIsEnabled)
					return false;
				if (this.PluginInstance == null)
					return false;
				if (this.PluginInstance.DataColumnCaption3 == null)
					return false;
				return true;
			}
		}

		#endregion
		#region PluginInstance

		private IPlugin _PluginInstance;
		public IPlugin PluginInstance
		{
			get
			{
				return _PluginInstance;
			}
			private set
			{
				_PluginInstance = value;
				OnPropertyChanged("PluginInstance", true, false);
				OnPropertyChanged("Title", true, false);
				OnPropertyChanged("DataColumnCaption", true, false);
				OnPropertyChanged("ProcessFilter", true, false);
				OnPropertyChanged("ShowEditView", true, false);
				OnPropertyChanged("ShowFilterView", true, false);
			}
		}

		#endregion

		#region ClearCustomFields

		public void ClearCustomFields()
		{
		}

		#endregion
		#region ImportCustomFields

		public void ImportCustomFields(Plugin source)
		{
			if (source == null)
				return;
			this.PluginInstance = source.PluginInstance;
		}

		#endregion
		#region InitializeAsync

		public async Task InitializeAsync()
		{
			await InitializeAsync(false);
		}
		public async Task InitializeAsync(bool initializeContext)
		{
			System.Type type;

			type = Sidvall.SystemContext.Current.TypeCache.GetType(this.TypeName);
			this.PluginInstance = Sidvall.SystemContext.Current.CodeManager.CreateInstance(type) as IPlugin;
			if (this.PluginInstance != null)
			{
				if ((initializeContext) && (!string.IsNullOrWhiteSpace(this.PluginInstance.DefaultContext)))
				{
					this.Context = this.PluginInstance.DefaultContext;
				}
				await this.PluginInstance.InitializeAsync(this.Context);
			}
		}

		#endregion

		#endregion
	}
}